import { Injectable } from '@nestjs/common';
import { CreateDialogDto } from './dto/create-dialog.dto';
import { UpdateDialogDto } from './dto/update-dialog.dto';
import * as fs from 'fs';
import * as path from 'path';
import { TemplateDialog } from './utils';
import {createLanguageModel,createTypeScriptJsonValidator,createJsonTranslator} from 'typechat'
import { Template } from './schema';

const libs = ['Button','Form','Table']
const types = libs.join(' | ')

const templates = libs.map(lib=>{
    return fs.readFileSync(path.join(__dirname,'./schema/lib',`${lib.toLowerCase()}.d.ts`), "utf8");
}).join('\n')

const schemaText = fs.readFileSync(path.join(__dirname,'./schema/index.d.ts'), "utf8").replace('export type Templates = Error;',`export type Templates = ${types} | Error;`)+'\n'+templates;
const model = createLanguageModel({ DEEPSEEK_API_KEY: 'sk-nqfxcttfftwmzvnpdsxfzreqpfmochllvzunyadlbqtiahwk', DEEPSEEK_MODEL: 'deepseek-ai/DeepSeek-V3' });
const validator = createTypeScriptJsonValidator<Template>(schemaText, "Template");
const translator = createJsonTranslator(model, validator);

const dialog = new TemplateDialog(translator);

@Injectable()
export class DialogService {
  create(createDialogDto: CreateDialogDto) {
    return dialog.process(createDialogDto.message);
  }

  findAll() {
    return `This action returns all dialog`;
  }

  findOne(id: number) {
    return `This action returns a #${id} dialog`;
  }

  update(id: number, updateDialogDto: UpdateDialogDto) {
    return `This action updates a #${id} dialog`;
  }

  remove(id: number) {
    return `This action removes a #${id} dialog`;
  }
}
